package im.starchat.chat.dialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.sina.weibo.SinaWeibo;
import im.starchat.chat.R;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.RoomBean;

/**
 * Created by skyfishjy on 1/6/15.
 */
public class SharePreviewDialogFragment extends DialogFragment {
    Dialog dialog;

    private static final String ROOM_BEAN = "room_bean";

    private RoomBean mRoomBean;

//    public static ShareAppDialogFragment newInstance() {
//        ShareAppDialogFragment comicDetailFragment = new ShareAppDialogFragment();
//        return comicDetailFragment;
//    }

    public static SharePreviewDialogFragment newInstance(RoomBean roomBean) {
        SharePreviewDialogFragment fragment = new SharePreviewDialogFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable(ROOM_BEAN, roomBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRoomBean = getArguments().getParcelable(ROOM_BEAN);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.dialog_share, null);
        builder.setView(view);
        dialog = builder.create();
        view.findViewById(R.id.share_to_weibo).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                new ShareAction(getActivity())

                SinaWeibo.ShareParams sp = new SinaWeibo.ShareParams();
                sp.setText(mRoomBean.getUser().getNickname() + "正在直播，快来和他一起聊天呀" + RestClient.BASE_DOMAIN);
                sp.setImageUrl(mRoomBean.getCover());
                sp.setSiteUrl(RestClient.BASE_DOMAIN);
                Platform weibo = ShareSDK.getPlatform(SinaWeibo.NAME);
                weibo.setPlatformActionListener(mPlatformActionListener); // 设置分享事件回调
                weibo.share(sp);

//
//                        .setPlatform(SHARE_MEDIA.SINA)
//                        .setCallback(uMShareListener)
//                        .withText(mRoomBean.getUser().getNickname() + "正在直播，快来和他一起聊天呀")
//                        .withMedia(new UMImage(getActivity(), mRoomBean.getCover()))
//                        .withTargetUrl(RestClient.BASE_DOMAIN)
////                        .withMedia(new UMImage(getActivity(), "http://www.umeng.com/images/pic/social/integrated_3.png"))
//                        .share();
            }
        });

        view.findViewById(R.id.share_to_wechat).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ShareAction(getActivity())
                        .setPlatform(SHARE_MEDIA.WEIXIN)
                        .setCallback(uMShareListener)
                        .withText(mRoomBean.getUser().getNickname() + "正在直播，快来和他一起聊天呀")
                        .withMedia(new UMImage(getActivity(), mRoomBean.getCover()))
                        .withTargetUrl(RestClient.BASE_DOMAIN)
//                        .withMedia(new UMImage(getActivity(), "http://www.umeng.com/images/pic/social/integrated_3.png"))
                        .share();
            }
        });

        view.findViewById(R.id.share_to_wechat_moment).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new ShareAction(getActivity())
                        .setPlatform(SHARE_MEDIA.WEIXIN_CIRCLE)
                        .setCallback(uMShareListener)
                        .withText(mRoomBean.getUser().getNickname() + "正在直播，快来和他一起聊天呀")
                        .withMedia(new UMImage(getActivity(), mRoomBean.getCover()))
                        .withTargetUrl(RestClient.BASE_DOMAIN)
//                        .withMedia(new UMImage(getActivity(), "http://www.umeng.com/images/pic/social/integrated_3.png"))
                        .share();
            }
        });
        view.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        return dialog;
    }

    PlatformActionListener mPlatformActionListener = new PlatformActionListener() {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            Toast.makeText(getActivity(),  " 分享成功", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {
            Toast.makeText(getActivity(), " 分享失败", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(Platform platform, int i) {
            Toast.makeText(getActivity(),  " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };

    UMShareListener uMShareListener = new UMShareListener() {
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), platform + " 分享成功啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(getActivity(), platform + " 分享失败啦", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(getActivity(), platform + " 分享取消了", Toast.LENGTH_SHORT).show();
        }
    };


}
